#!/bin/bash

#df检查磁盘使用情况
function GetSysSpace
{
    AvgUsedSpace=`df -h|grep -v Use|awk '{x=x+$5;y=y+1}END{print x/y}'`
    df -h >> status.log
    echo "Space have used $AvgUsedSpace%" >> status.log
}

#free检查内存使用情况
function GetSysMem
{
    MemFree=`free -m|grep Mem|awk '{print $4}'`
    free -m >> status.log
    echo "The free Mem is $MemFree M" >> status.log
}

#top检查cpu负载
function GetSysCpu
{
    freeCpu=`top -n 1|grep Cpu|awk -F "," '{print $4}'|awk -F "id" '{print $1}'`
    top -b -n 1 >> status.log
    echo "The percentage of free CPU is $freeCpu%" >> status.log
}

#记录当前时间
datetime=`date +"%Y-%m-%d %H:%M:%S"`
echo $datetime >> status.log
echo "磁盘检查：" >> status.log
GetSysSpace
echo "内存检查：" >> status.log
GetSysMem
echo "cpu负载检查：" >> status.log
GetSysCpu
#记录当前登录用户
echo "当前登录用户:" >> status.log
last >> status.log

